// 主要JavaScript功能
document.addEventListener("DOMContentLoaded", function () {
  // 初始化所有功能
  initSlider();
  initMobileMenu();
  initSmoothScroll();
});

// 幻灯片功能
function initSlider() {
  const slides = document.querySelectorAll(".slide");
  const navDots = document.querySelectorAll(".nav-dot");
  let currentSlide = 0;

  if (slides.length === 0) return;

  // 显示指定的幻灯片
  function showSlide(index) {
    slides.forEach((slide) => slide.classList.remove("active"));
    navDots.forEach((dot) => dot.classList.remove("active"));

    slides[index].classList.add("active");
    if (navDots[index]) {
      navDots[index].classList.add("active");
    }
  }

  // 下一张幻灯片
  function nextSlide() {
    currentSlide = (currentSlide + 1) % slides.length;
    showSlide(currentSlide);
  }

  // 导航点点击事件
  navDots.forEach((dot, index) => {
    dot.addEventListener("click", () => {
      currentSlide = index;
      showSlide(currentSlide);
    });
  });

  // 自动播放 (6秒间隔，速度更慢)
  const autoPlayInterval = setInterval(nextSlide, 6000);

  // 鼠标悬停时暂停自动播放
  const slider = document.querySelector(".slider");
  if (slider) {
    slider.addEventListener("mouseenter", () =>
      clearInterval(autoPlayInterval),
    );
    slider.addEventListener("mouseleave", () => setInterval(nextSlide, 6000));
  }

  // 初始显示第一张
  showSlide(0);
}

// 移动端菜单
function toggleMobileMenu() {
  const navMenu = document.querySelector(".nav-menu");
  const mobileBtn = document.querySelector(".mobile-menu-btn");

  if (navMenu.style.display === "flex") {
    navMenu.style.display = "none";
    mobileBtn.classList.remove("active");
  } else {
    navMenu.style.display = "flex";
    navMenu.style.flexDirection = "column";
    navMenu.style.position = "absolute";
    navMenu.style.top = "70px";
    navMenu.style.left = "0";
    navMenu.style.right = "0";
    navMenu.style.backgroundColor = "white";
    navMenu.style.padding = "20px";
    navMenu.style.boxShadow = "0 4px 6px rgba(0, 0, 0, 0.1)";
    mobileBtn.classList.add("active");
  }
}

function initMobileMenu() {
  const mobileBtn = document.querySelector(".mobile-menu-btn");
  if (mobileBtn) {
    mobileBtn.addEventListener("click", toggleMobileMenu);
  }

  // 点击页面其他地方关闭菜单
  document.addEventListener("click", function (e) {
    const navMenu = document.querySelector(".nav-menu");
    const mobileBtn = document.querySelector(".mobile-menu-btn");
    const navbar = document.querySelector(".navbar");

    if (!navbar.contains(e.target) && navMenu.style.display === "flex") {
      navMenu.style.display = "none";
      if (mobileBtn) mobileBtn.classList.remove("active");
    }
  });
}

// 平滑滚动
function initSmoothScroll() {
  document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
    anchor.addEventListener("click", function (e) {
      e.preventDefault();
      const target = document.querySelector(this.getAttribute("href"));
      if (target) {
        target.scrollIntoView({
          behavior: "smooth",
          block: "start",
        });
      }
    });
  });
}